package com.xiaomaoguai.mybatisplus.generator;

import com.xiaomaoguai.mybatisplus.utils.SystemConfig;
import org.aeonbits.owner.ConfigFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

/**
 * * <p>
 * 生成器配置类
 * ********************************* 使用前必读 *********************
 * saveDir 文件生成目录
 * entity_package entity 包路径
 * mapper_package mapper 包路径
 * xmlPackage xx_mapper.xml 包路径，默认为mapper/xml
 * servicePackage service 包路径
 * serviceImplPackage serviceImpl包路径，默认为service/impl
 * superService 	service 父类包路径名称
 * superServiceImpl service 实现父类包路径名称
 * mapperName		自定义 mapper 名称
 * mapperXMLName	自定义 xml 名称
 * serviceName		自定义 service 名称
 * serviceImplName	自定义 serviceImp 名称
 * tableNames   	要生成的表名称，如为空就直接指定所有表.格式为逗号分割
 * fileOverride 	是否覆盖当前已有文件
 * -------------------------------------
 * 以下数据库相关配置：
 * -------------------------------------
 * db_include_prefix 表是否包含前缀，例如: tb_xxx 其中 tb_ 为前缀
 * db_driverName 驱动
 * db_user 用户名
 * db_password 密码
 * db_url 连接地址
 * *************************************************************
 * </p>
 *
 * @fileName: GeneratorConfig.java
 * @author: WeiHui.Zhang
 * @date: 2016-09-22  14:38
 * @version: v1.0.0
 */
public class GeneratorConfig {

	/**
	 * 模板路径
	 */
	String TEMPLATE_PATH = SystemConfig.class.getResource("/").getFile() + "/template";

	/**
	 * 创建时间
	 */
	String createdTime = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss").format(new Date());

	protected String saveDir;

	protected String entityPackage;

	protected String mapperPackage;

	protected String mapperXmlPackage;

	protected String servicePackage;

	protected String serviceImplPackage;

	protected String serverPackage;

	protected String serverImplPackage;

	protected String controllerPackage;

	/**
	 * 指定生成表名
	 */
	protected Set<String> includedTables = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
	/**
	 * 指定排除表名
	 */
	protected Set<String> excludedTables = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);

	/**
	 * 是否覆盖当前路径下已有文件（默认 true）
	 */
	protected boolean fileOverride = true;

	/**
	 * 表名前缀
	 */
	protected String[] tablePrefix;

	protected String dbDriverName;

	protected String dbUser;

	protected String dbPassword;

	protected String dbUrl;

	protected ConfigDataSource configDataSource;

	public String getSaveDir() {
		return saveDir;
	}

	public void setSaveDir(String saveDir) {
		this.saveDir = saveDir;
	}

	public String getEntityPackage() {
		return entityPackage;
	}

	public void setEntityPackage(String entityPackage) {
		this.entityPackage = entityPackage;
	}

	public String getMapperPackage() {
		return mapperPackage;
	}

	public void setMapperPackage(String mapperPackage) {
		this.mapperPackage = mapperPackage;
	}

	public String getMapperXmlPackage() {
		return mapperXmlPackage;
	}

	public void setMapperXmlPackage(String mapperXmlPackage) {
		this.mapperXmlPackage = mapperXmlPackage;
	}

	public String getServicePackage() {
		return servicePackage;
	}

	public void setServicePackage(String servicePackage) {
		this.servicePackage = servicePackage;
	}

	public String getServiceImplPackage() {
		return serviceImplPackage;
	}

	public void setServiceImplPackage(String serviceImplPackage) {
		this.serviceImplPackage = serviceImplPackage;
	}

	public String getServerPackage() {
		return serverPackage;
	}

	public void setServerPackage(String serverPackage) {
		this.serverPackage = serverPackage;
	}

	public String getServerImplPackage() {
		return serverImplPackage;
	}

	public void setServerImplPackage(String serverImplPackage) {
		this.serverImplPackage = serverImplPackage;
	}

	public String getControllerPackage() {
		return controllerPackage;
	}

	public void setControllerPackage(String controllerPackage) {
		this.controllerPackage = controllerPackage;
	}

	public Set<String> getIncludedTables() {
		return includedTables;
	}

	public void addIncludedTables(String... tables) {
		if (tables != null) {
			for (String table : tables) {
				this.includedTables.add(table);
			}
		}
	}

	public Set<String> getExcludedTables() {
		return excludedTables;
	}

	public void addExcludedTables(String... tables) {
		if (tables != null) {
			for (String table : tables) {
				this.excludedTables.add(table);
			}
		}
	}

	public boolean isFileOverride() {
		return fileOverride;
	}

	public void setFileOverride(boolean fileOverride) {
		this.fileOverride = fileOverride;
	}

	public String[] getTablePrefix() {
		return tablePrefix;
	}

	public void setTablePrefix(String[] tablePrefix) {
		this.tablePrefix = tablePrefix;
	}

	public String getDbDriverName() {
		return dbDriverName;
	}

	public void setDbDriverName(String dbDriverName) {
		this.dbDriverName = dbDriverName;
		if (dbDriverName.contains("mysql")) {
			this.configDataSource = ConfigDataSource.MYSQL;
		}
		if (dbDriverName.contains("oracle")) {
			this.configDataSource = ConfigDataSource.ORACLE;
		}
	}

	public String getDbUser() {
		return dbUser;
	}

	public void setDbUser(String dbUser) {
		this.dbUser = dbUser;
	}

	public String getDbPassword() {
		return dbPassword;
	}

	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}

	public String getDbUrl() {
		return dbUrl;
	}

	public void setDbUrl(String dbUrl) {
		this.dbUrl = dbUrl;
	}

	public ConfigDataSource getConfigDataSource() {
		return configDataSource;
	}

	public void setConfigDataSource(ConfigDataSource configDataSource) {
		this.configDataSource = configDataSource;
	}

	/**
	 * 将配置文件里的配置统一处理
	 */
	public void load() {
		SystemConfig systemConfig = ConfigFactory.create(SystemConfig.class);
	}
}